/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cp;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import vn.webapp.controller.BaseWeb;
import vn.webapp.dto.DataPage;
import vn.webapp.form.TeacherValidationForm;
import vn.webapp.model.Sessions;
import vn.webapp.model.Teachers;
import vn.webapp.service.SessionsService;
import vn.webapp.service.TeachersService;
/**
 *
 * @author Tonytran
 */
@Controller("cpTeachers")
@RequestMapping(value = {"/cp"})
public class TeachersController extends BaseWeb{
    
    @Autowired
    private TeachersService teachersService;
    
    @Autowired
    private SessionsService sessionsService;
    
    private int nav = 2;
    /**
     * 
     * @param model
     * @return 
     */
    @RequestMapping(value = "/teachers" , method = RequestMethod.GET)
    public String classes(ModelMap model, HttpSession session,
    @RequestParam(value = "page", defaultValue = "0") int page){
        int pageSize = 10;  
        DataPage<Teachers> dataTeachers = teachersService.filter(page, pageSize);
        //DataPage<Teachers> dataTeachers = teachersService.list();
        model.put("sz_Response", session.getAttribute("response"));
        session.setAttribute("response", "");
        model.put("dataTeachers", dataTeachers);
        model.put("nav", this.nav);
        model.put("pageIndex", page);
        return "cp.teachers";
    }
    
    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/teacherdetail/{id}")
    public String viewDetail(ModelMap model, @PathVariable("id") String id) {
        Teachers teacher = teachersService.viewDetail(id);
        model.put("teacherForm", new TeacherValidationForm());
        model.put("dataTeacher", teacher);
        model.put("nav", this.nav);
        return "cp.teacherdetail";
    }
    

    @RequestMapping(value = "/saveteacherdetail", method = RequestMethod.POST)
    public String processEditClass(@Valid @ModelAttribute("teacherForm") TeacherValidationForm teacherForm,
            BindingResult result,HttpServletRequest request, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.teacherdetail";
        }else
        {
            String TeacherName = teacherForm.getName();
            String TeacherEmail = teacherForm.getEmail();
            int TeacherId = teacherForm.getId();
            Teachers o_Teacher = teachersService.checkExistsEmail(TeacherEmail, TeacherId);
            if(o_Teacher != null)
            {
                //model.put("sz_Response", "The email "+TeacherEmail+" is being used.");
                result.rejectValue("email", "messageCode","The email "+TeacherEmail+" is being used.");                
                return "cp.teacherdetail";
            }else{
                int i_UpdateTeacher = teachersService.updateTheTeacher(TeacherName, TeacherEmail, TeacherId);
                session.setAttribute("response", "Edit successfully!");
                return "redirect:" + this.baseUrl + "/cp/teachers.html";
            }
        }
    }
    
    @RequestMapping(value = "/removeteacher/{id}", method = RequestMethod.GET)
    public String processRemoveTeacher(ModelMap model, @PathVariable("id") int id, HttpSession session) {
        int i_IsRemoved = teachersService.removeTeacher(id);
        String sz_Response;
        if (i_IsRemoved != 0) {
            sz_Response = "Remove the teacher successfully !";
        } else {
            sz_Response = "Remove the teacher not successfully. Please retry";
        }
        session.setAttribute("response", sz_Response);
        return "redirect:" + this.baseUrl + "/cp/teachers.html";
    }
    
    @RequestMapping(value = "/addnewteacher", method = RequestMethod.GET)
    public String addnewteacher(Map model){
        model.put("nav", this.nav);
        model.put("teacherFormAdd", new TeacherValidationForm());
        return "cp.addnewteacher";
    }
    
    @RequestMapping(value = "/setbusy", method = RequestMethod.GET)
    public String setBusy(Map model){
        model.put("nav", this.nav);
        model.put("teacherFormAdd", new TeacherValidationForm());
        DataPage<Teachers> dataTeachers = teachersService.list();
        
        DataPage<Sessions> dataSessions = sessionsService.list();
        
        model.put("teachers", dataTeachers.getData());
        model.put("sessions", dataSessions.getData());
        
        return "cp.setbusy";
    }
    
    @RequestMapping(value = "/saveteacher", method = RequestMethod.POST)
    public String processAddNewTeacher(@Valid @ModelAttribute("teacherFormAdd") TeacherValidationForm teacherFormAdd,
            BindingResult result, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.addnewteacher";
        }else
        {
            String TeacherName = teacherFormAdd.getName();
            String TeacherEmail = teacherFormAdd.getEmail();
            int TeacherId = teacherFormAdd.getId();
            Teachers o_Teacher = teachersService.checkExistsEmail(TeacherEmail, TeacherId);
            if(o_Teacher != null)
            {
                model.put("sz_Response", "The email "+TeacherEmail+" is being used.");
                return "cp.addnewteacher";
            }else
            {
                int i_InsertATeacher = teachersService.saveATeacher(TeacherName, TeacherEmail);
                session.setAttribute("response", "Add successfully!");
                return "redirect:" + this.baseUrl + "/cp/teachers.html";
            }
        }
    }
}
